package spring.core.aop.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.util.ReflectionUtils;

/**
 * @author 1203056
 * @version Id: TeacherAspect.java, v 0.1 2021-09-07 9:44 1203056 Exp $
 */
@Aspect
@Order(value = Ordered.HIGHEST_PRECEDENCE + 1)
public class RootAspect {

    @Pointcut("execution(public * *(..))")
    private void pointcut() {
    }

    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {

        try {
            if (joinPoint instanceof MethodInvocationProceedingJoinPoint) {
                MethodInvocationProceedingJoinPoint methodJoinPoint = (MethodInvocationProceedingJoinPoint) joinPoint;
                MethodSignature methodSignature = (MethodSignature) methodJoinPoint.getSignature();
                AspectContext.setCurrentMethod(methodSignature.getMethod());
                if (ReflectionUtils.isObjectMethod(methodSignature.getMethod())) {
                    System.out.println("直接返回，invoke object method: " + methodSignature.getMethod());
                    return;
                }
            }
            joinPoint.proceed(joinPoint.getArgs());
        } finally {
            AspectContext.remove();
        }
    }

}
